package com.macro.mall.tiny.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.domain.PmsBrand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * HutoolController
 * Created by jiachun.lu on 2025/02/10.
 */
@RestController
@Api(tags = "HutoolController")
@Tag(name = "HutoolController", description = "Hutool工具类测试")
@RequestMapping("/hutool")
public class HutoolController {

    private static Logger LOGGER = LoggerFactory.getLogger(HutoolController.class);

    @ApiOperation("Convert使用：类型转换工具类")
    @GetMapping(value = "/convert")
    public CommonResult convert() {
        int a = 1;
        String aStr = Convert.toStr(a);

        String[] b = {"1", "2", "3", "4"};
        Integer[] bInt = Convert.toIntArray(b);

        String dateStr = "2017-05-06";
        Date date = Convert.toDate(dateStr);

        String[] strArr = {"a", "b", "c", "d"};
        List<String> strList = Convert.toList(String.class, strArr);

        return CommonResult.success(null, "操作成功");
    }


    @ApiOperation("DateUtil使用：日期工具类")
    @GetMapping(value = "/dateUtil")
    public CommonResult dateUtil() {
        Date date = DateUtil.date();
        date = DateUtil.date(Calendar.getInstance());
        date = DateUtil.date(System.currentTimeMillis());

        String dateStr = "2017-03-01";
        date = DateUtil.parse(dateStr);
        date = DateUtil.parse(dateStr, "yyyy-MM-dd");
        String format = DateUtil.format(date, "yyyy-MM-dd");
        int year = DateUtil.year(date);
        int month = DateUtil.month(date);

        Date beginOfDay = DateUtil.beginOfDay(date);
        Date endOfDay = DateUtil.endOfDay(date);

        Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
        long betweenDay = DateUtil.between(date, newDate, DateUnit.DAY);
        return CommonResult.success(null, "操作成功");
    }

    @ApiOperation("StrUtil使用：字符串工具类")
    @GetMapping(value = "/strUtil")
    public CommonResult strUtil() {
        String str = "test";
        StrUtil.isEmpty(str);
        StrUtil.isNotEmpty(str);
        StrUtil.removeSuffix("a.jpg", ".jpg");
        StrUtil.removePrefix("a.jpg", "a.");

        String template = "这只是个占位符:{}";
        String str2 = StrUtil.format(template, "占位符");
        LOGGER.info("strUtil format:{}", str2);
        return CommonResult.success(null, "操作成功");
    }

    @ApiOperation("classPath资源访问：在classpath下查找文件")
    @GetMapping(value = "/classPath")
    public CommonResult classPath() throws IOException {
        ClassPathResource resource = new ClassPathResource("generator.properties");
        Properties properties = new Properties();
        properties.load(resource.getStream());
        LOGGER.info("classPath:{}", properties);
        return CommonResult.success(null, "操作成功");
    }

    @ApiOperation("ReflectUtil使用")
    @GetMapping(value = "/reflectUtil")
    public CommonResult reflectUtil() {
        Method[] methods = ReflectUtil.getMethods(PmsBrand.class);
        Method method = ReflectUtil.getMethod(PmsBrand.class, "getId");
        PmsBrand pmsBrand = ReflectUtil.newInstance(PmsBrand.class);
        ReflectUtil.invoke(pmsBrand, "setId", 1L);
        return CommonResult.success(null, "操作成功");
    }


}
